package bb.lanxing.lib.devices.core.utils;

import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.List;

import android.os.Parcel;

import bb.lanxing.lib.devices.api.ConnectionListener;
import bb.lanxing.lib.devices.api.DeviceApplication;
import bb.lanxing.lib.devices.api.DeviceManager;
import bb.lanxing.lib.devices.api.DeviceProvider;
import bb.lanxing.lib.devices.api.DeviceStateListener;
import bb.lanxing.lib.devices.api.SmartDevice;

public final class DeviceHelper {
    private static WeakReference<DeviceApplication> deviceApplicationRef;

    public static DeviceApplication getDeviceApplication() {
        WeakReference<DeviceApplication> weakReference = deviceApplicationRef;
        if (weakReference != null) {
            return weakReference.get();
        }
        return null;
    }

    public static void setDeviceApplication(DeviceApplication deviceApplication) {
        deviceApplicationRef = new WeakReference<>(deviceApplication);
    }

    public static DeviceManager getDeviceManager() {
        WeakReference<DeviceApplication> weakReference = deviceApplicationRef;
        DeviceApplication deviceApplication = weakReference != null ? weakReference.get() : null;
        if (deviceApplication != null) {
            return deviceApplication.getDeviceManager();
        }
        return null;
    }

    public static String getWorkDir(int i) {
        DeviceManager deviceManager = getDeviceManager();
        if (deviceManager != null) {
            return deviceManager.getWorkDir(i);
        }
        return null;
    }

    public static DeviceProvider getDeviceProvider() {
        DeviceApplication deviceApplication = getDeviceApplication();
        if (deviceApplication != null) {
            return deviceApplication.getDeviceProvider();
        }
        return null;
    }

    public static void registerConnectionStateListener(ConnectionListener connectionListener) {
        DeviceManager deviceManager = getDeviceManager();
        if (deviceManager != null) {
            deviceManager.registerConnectionStateListener(connectionListener);
        }
    }

    public static void unregisterConnectionStateListener(ConnectionListener connectionListener) {
        DeviceManager deviceManager = getDeviceManager();
        if (deviceManager != null) {
            deviceManager.unregisterConnectionStateListener(connectionListener);
        }
    }

    public static void registerDeviceStateListener(DeviceStateListener deviceStateListener) {
        DeviceManager deviceManager = getDeviceManager();
        if (deviceManager != null) {
            deviceManager.registerDeviceStateListener(deviceStateListener);
        }
    }

    public static void unregisterDeviceStateListener(DeviceStateListener deviceStateListener) {
        DeviceManager deviceManager = getDeviceManager();
        if (deviceManager != null) {
            deviceManager.unregisterDeviceStateListener(deviceStateListener);
        }
    }

    public static SmartDevice createDeviceFromByteArray(byte[] bArr) {
        DeviceProvider deviceProvider = getDeviceProvider();
        if (bArr == null || deviceProvider == null) {
            return null;
        }
        Parcel obtain = Parcel.obtain();
        obtain.unmarshall(bArr, 0, bArr.length);
        obtain.setDataPosition(0);
        SmartDevice create = deviceProvider.create();
        if (create != null) {
            create.readFromParcel(obtain);
        }
        obtain.recycle();
        return create;
    }

    public static SmartDevice createDevice() {
        DeviceProvider deviceProvider = getDeviceProvider();
        if (deviceProvider != null) {
            return deviceProvider.create();
        }
        return null;
    }

    public static byte[] createByteArrayFromDevice(SmartDevice smartDevice) {
        Parcel obtain = Parcel.obtain();
        smartDevice.writeToParcel(obtain, 0);
        byte[] marshall = obtain.marshall();
        obtain.recycle();
        return marshall;
    }

    public static boolean isConnected(int deviceType) {
        DeviceManager deviceManager = getDeviceManager();
        return deviceManager != null && deviceManager.isConnected(deviceType);
    }

    public static boolean isConnected(String deviceAddress) {
        DeviceManager deviceManager = getDeviceManager();
        return deviceManager != null && deviceManager.isConnected(deviceAddress);
    }

    public static boolean isConnecting(SmartDevice smartDevice) {
        DeviceManager deviceManager = getDeviceManager();
        if (deviceManager != null) {
            return deviceManager.isConnecting(smartDevice.getAddress());
        }
        return false;
    }

    public static boolean isConnecting(String str) {
        DeviceManager deviceManager = getDeviceManager();
        if (deviceManager != null) {
            return deviceManager.isConnecting(str);
        }
        return false;
    }

    public static void disconnect(int i) {
        DeviceManager deviceManager = getDeviceManager();
        if (deviceManager != null) {
            deviceManager.disconnect(i);
        }
    }

    public static void disconnect(String str) {
        DeviceManager deviceManager = getDeviceManager();
        if (deviceManager != null) {
            deviceManager.disconnect(str);
        }
    }

    public static void disconnectAll() {
        DeviceManager deviceManager = getDeviceManager();
        if (deviceManager != null) {
            deviceManager.disconnectAll();
        }
    }

    public static void connect(SmartDevice smartDevice) {
        DeviceManager deviceManager = getDeviceManager();
        if (deviceManager != null) {
            deviceManager.connect(smartDevice);
        }
    }

    public static String getDeviceAddressByType(int i) {
        List<SmartDevice> devicesByType;
        DeviceManager deviceManager = getDeviceManager();
        DeviceProvider deviceProvider = getDeviceProvider();
        if (deviceManager == null || deviceProvider == null
                || (devicesByType = deviceProvider.getDevicesByType(i)) == null || devicesByType.isEmpty()) {
            return null;
        }
        Collections.reverse(devicesByType);
        for (SmartDevice smartDevice : devicesByType) {
            String address = smartDevice.getAddress();
            if (deviceManager.isConnected(address)) {
                return address;
            }
        }
        return devicesByType.get(0).getAddress();
    }

    public static List<SmartDevice> getDevicesByType(int i) {
        DeviceProvider deviceProvider = getDeviceProvider();
        if (deviceProvider == null) {
            return null;
        }
        return deviceProvider.getDevicesByType(i);
    }
}
